﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace ParseControls
{
    public class FwTextBox : TextBox
    {
        TextBlock tb = null;

        public double BackgroundTextFontSize
        {
            get { return (double)GetValue(BackgroundTextFontSizeProperty); }
            set { SetValue(BackgroundTextFontSizeProperty, value); }
        }
        public static readonly DependencyProperty BackgroundTextFontSizeProperty =
            DependencyProperty.Register("BackgroundTextFontSize", typeof(double),
            typeof(FwTextBox), new PropertyMetadata(11.0));


        public Brush BackgroundTextBrush
        {
            get { return (Brush)GetValue(BackgroundTextBrushProperty); }
            set { SetValue(BackgroundTextBrushProperty, value); }
        }
        public static readonly DependencyProperty BackgroundTextBrushProperty =
            DependencyProperty.Register("BackgroundTextBrush", typeof(Brush),
            typeof(FwTextBox), new PropertyMetadata(Brushes.White));

        public object BackgroundText
        {
            get { return (object)GetValue(BackgroundTextProperty); }
            set { SetValue(BackgroundTextProperty, value); }
        }
        public static readonly DependencyProperty BackgroundTextProperty =
            DependencyProperty.Register("BackgroundText", typeof(object),
            typeof(FwTextBox), new PropertyMetadata(null));


        public FwTextBox()
            : base()
        { }
        static FwTextBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(FwTextBox),
                new FrameworkPropertyMetadata(typeof(FwTextBox)));
        }


        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            tb = this.Template.FindName("PART_BackgroundTextHost", this) as TextBlock;
            this.TextChanged += FwTextBox_TextChanged;

        }
        void FwTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            tb.Visibility = System.Windows.Visibility.Visible;
            if (this.Text.Length >= 1)
            {
                tb.Visibility = System.Windows.Visibility.Collapsed;
            }
        }
    }
}
